/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = test_ora_comm.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/10 by songbb                               */
/*******************************************************************/

#include <stdio.h>

#include "conntrack.h"
#include "frct_comm.h"

#include <oracle/comm/ora_comm.h>
#include <oracle/tns/ora_packet.h>
#include "apmtest_comm.h"
#include "test_ora_comm.h"

extern int __ora_private_session_init(L7_session *p_st_session);
extern int ora_session_destory(void *p_st_session_private_in);

int test_ora_create_ct(Conntrack **pp_st_conn, char *src_ip,
                        char *dst_ip, unsigned short src_port, unsigned short dst_port)
{
    Conntrack *p_st_conn = NULL;
    L7_session *p_st_session = NULL;
    if (apmtest_create_ct(&p_st_conn, ora_session_destory, src_ip, dst_ip, src_port, dst_port) != FR_SUCC)
        goto error;

    p_st_session = frct_get_session(p_st_conn);
    if (__ora_private_session_init(p_st_session) != FR_SUCC)
        goto error;

    *pp_st_conn = p_st_conn;

    return FR_SUCC;
error:
    return FR_FAIL;
}

void test_ora_destroy_ct(Conntrack **pp_st_conn)
{
    apmtest_destroy_ct(pp_st_conn);
}

L7session_ora* test_ora_session_create(void)
{
    L7session_ora *p_st_ora_sess = NULL;
    if ((p_st_ora_sess = (L7session_ora *) frmalloc(sizeof(L7session_ora))) == NULL)
        return NULL;
    memset(p_st_ora_sess, 0x00, sizeof(L7session_ora));

    ora_set_ttc_type_rep_jdbc(p_st_ora_sess);
    ora_set_ttc_type_rep(p_st_ora_sess, 1, 2);
    ora_reset_sess_phase(p_st_ora_sess->arr_sess_phase);
    p_st_ora_sess->i_charset_assumed                  = FR_NO;
    p_st_ora_sess->s_tns_version_req                  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->s_tns_version                      = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_srv_is_bigendian                 = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->c_conversion_flags                 = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_is_large_sdu                     = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_srv_conversion                   = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->c_ttc_version                      = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_useCLR_big_chunks                = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_useLobPrefetch                   = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->c_pro_srv_version                  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->s_version_number                   = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_pool_enabled                     = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ptr_len                          = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_use_oci                          = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_use_special_field                = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_send_typerep                     = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_c2snlsratio                      = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_oci_stmt_req_skip4_ttc7          = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_oci_stmt_req_skip4_ttc8          = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_oci_oversion_skip4               = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->c_ttc_api_version                  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->p_list_closed_cursors              = frlist_create();

    p_st_ora_sess->i_CHR_conversion_flags             = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc7_oactoid                     = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc7_rxhrqsts                    = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_long_analyze_succ           = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_req_i                  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_req_j                  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_req_dml                = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_req_oci_after_dml      = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_dcb_recv          = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oac_skip1         = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oac_skipaftervsn  = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_udskpos           = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_udsflg            = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_dcbkm             = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_dcbni1            = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_dcbab2            = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_rpag              = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_rpadml            = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oereocs           = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oerendseq         = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oeraij            = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oci_oerskip8      = APM_ORA_FLAG_UNSET;
    p_st_ora_sess->i_ttc8_stmt_resp_oci_oerskip6      = APM_ORA_FLAG_UNSET;

    return p_st_ora_sess;
}

L7_ora_packet* test_ora_packet_create()
{
    L7_ora_packet *p_st_ora_packet = NULL;
    if ((p_st_ora_packet = (L7_ora_packet *) frmalloc(sizeof(L7_ora_packet))) == NULL)
        return NULL;
    memset(p_st_ora_packet, 0x00, sizeof(L7_ora_packet));
    if (ora_init_ora_packet(p_st_ora_packet) != FR_SUCC)
    {
        ora_free((void **) &p_st_ora_packet);
        return NULL;
    }

    return p_st_ora_packet;
}
